package com.omg.server.crawler.strategy.impl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.omg.business.model.PostMeta;
import com.omg.business.model.Posts;
import com.omg.server.crawler.RequestContext;
import com.omg.server.crawler.strategy.CrawlerStrategy;
import com.omg.server.framework.service.IDefaultService;
import com.omg.server.util.CommonUtil;
import com.omg.server.util.DateUtil;
import com.omg.server.util.HttpSenderUtil;
import com.omg.weibo.WeiBoSender;

/**
 * 关于什么值得买 网站抓取策略
 * @author Administrator
 */
public class ZhiZhiZhiStrategyImpl implements CrawlerStrategy{
	private static final Log log = LogFactory.getLog(ZhiZhiZhiStrategyImpl.class);
	private IDefaultService defaultService;
	@Override
	public void handle(RequestContext requestContext) {
		int pageIndex = requestContext.getStartIndex();
		if (pageIndex < 1){
			return;
		}
		String url = requestContext.getSiteUrl();
		String soureUrl = requestContext.getSiteUrl();
		log.info(url + "第" + pageIndex + "抓取.");
		
		Source source = null;
		try {
			String subStract = null;
			String imgUrl = null;
			String imgUrl2 = null;
			String targetUrl = null;
			Element bizElement = null;
			Element subElement = null;
			Element subElement2 = null;
			String relativeUrl = null;
			Posts posts = null;
			PostMeta postMeta = null;
			PostMeta postMeta2 = null;
			List<Element> bizList = null;
			String price = null;
			String title = null;
			String issueTime = null;
			source = new Source(new URL(url+"page/"+pageIndex));
			// Call fullSequentialParse manually as most of the source will be parsed.
			source.fullSequentialParse();
			String pageUrl = null;
			List<Element> linkList = null;
			List<Element> cata = null;
			String linkContent = null;
			bizList = source.getAllElementsByClass("post type-post status-publish");
			Date issueDate = null;
			List<Integer> count = null;
			String type = null;
			String tag = null;
			Map<String, Object> params = new HashMap<String, Object>(2);
			HashSet<PostMeta> pmSet = new HashSet<PostMeta>(2);
			URL hostUrl = null;
			
			if (CommonUtil.isNotEmpty(bizList)){
				WeiBoSender wbs = new WeiBoSender();
				for (Element el : bizList){
					try{
						title = null;
						price = null;
						subStract = null;
						targetUrl = null;
						issueDate = null;
						type = null;
						imgUrl = null;
						imgUrl2 = null;
						issueTime = null;
						pageUrl = null;
						relativeUrl = null;
						posts = new Posts();
						postMeta = new PostMeta();
						postMeta2 = new PostMeta();
						subElement = el.getFirstElement(HTMLElementName.H2);
						if (null != subElement){
							subElement = subElement.getFirstElement(HTMLElementName.A);
							//分离出价格
							title = subElement.getTextExtractor().toString();
							price = CommonUtil.getPrice(title);
							posts.setPostTitle(title);
							if (null == price){
								price = "";
							}
							
							subElement = subElement.getFirstElementByClass("title_end");
							if (null != subElement){
							    price = subElement.getTextExtractor().toString();
							    if (null != title){
							        title = title.replace(price, "");
							    }
							}
							posts.setPostTitle(title);
							posts.setPrice(price);
						}
						
						subElement = el.getFirstElementByClass("postmeta");
						if (null == subElement){
							continue;
						}
						subElement2 = subElement.getFirstElementByClass("folder");
						if (null != subElement2){
							cata = subElement2.getAllElements(HTMLElementName.A); 
							if (CommonUtil.isNotEmpty(cata)){
								type = "";
								for (Element e : cata){
									type += e.getTextExtractor() + ",";
								}
								if (type.endsWith(",")){
									type = type.substring(0, type.length()-1);
								}
							}
						}
						
						subElement2 = subElement.getFirstElementByClass("date");
						if (null != subElement2){
							//自动加年月日
							issueTime = subElement2.getContent().toString();
							if (CommonUtil.isNotEmpty(issueTime)){
								issueTime = issueTime.trim();
								issueTime = issueTime.replaceAll("年", "-").replaceAll("月", "-").replaceAll("日", "");
							}
							else{
								continue;
							}
							issueTime = issueTime + " " + DateUtil.formatDate(new Date(), DateUtil.HHMM);
							issueDate = DateUtil.convertToDate(issueTime, DateUtil.PATTERN_NOSECOND);
							if (null != requestContext.getMaxDate() && null != issueDate){
								if (issueDate.before(requestContext.getMaxDate())){
									continue;
								}
							}
							posts.setPostDate(issueDate);
							posts.setPostDate2(issueDate);
							posts.setPostModified(issueDate);
							posts.setPostModified2(issueDate);
						}
						
						subElement = el.getFirstElementByClass("postinfo");
						if (null != subElement){
							subElement2 = subElement.getFirstElementByClass("tag");
							cata = subElement2.getAllElements(HTMLElementName.A); 
							if (CommonUtil.isNotEmpty(cata)){
								tag = "";
								for (Element e : cata){
									tag += e.getTextExtractor() + ",";
								}
								if (tag.endsWith(",")){
									tag = tag.substring(0, tag.length()-1);
								}
							}
						}
		
						subElement = el.getFirstElementByClass("entry");
						if (null != subElement){
							bizElement = subElement.getFirstElement(HTMLElementName.A);
							relativeUrl = bizElement.getAttributeValue("href");
							if (null != relativeUrl && relativeUrl.startsWith("/")){
								relativeUrl = relativeUrl.substring(1);
							}
							pageUrl = url + relativeUrl;
							hostUrl = new URL(url);
							targetUrl = HttpSenderUtil.getTargetUrl(pageUrl);//商品目标来源
							for(int loop=0; loop < 3; loop++){ //只取三次
							    if (targetUrl.indexOf(hostUrl.getHost()) > -1){
								    targetUrl = HttpSenderUtil.getTargetUrl(targetUrl);//商品目标来源
							    }
							    else{
							    	break;
							    }
							}
							bizElement = subElement.getFirstElement(HTMLElementName.IMG);
							imgUrl = bizElement.getAttributeValue("src");//商品图片地址
							if (null !=imgUrl && imgUrl.indexOf("http:") < 0){
								if (imgUrl.startsWith("/")){
								   imgUrl = soureUrl.substring(0, soureUrl.length()-1) + imgUrl;
								}
								else{
									imgUrl = soureUrl + imgUrl;
								}
							}
							
							bizElement = subElement.getFirstElement(HTMLElementName.P);
							subStract = bizElement.getContent().toString();//商品摘要
							linkList = bizElement.getAllElements(HTMLElementName.A);
							if (CommonUtil.isNotEmpty(linkList) && null != subStract){
								for (Element cl : linkList){
									linkContent = cl.getTextExtractor().toString();
									subStract = subStract.replace(cl.toString(), linkContent);
									//String linkContent2 = linkContent;
									//if (null != linkContent2 &&!linkContent2.endsWith("/")){
										//linkContent2 += "/";
									//}
									//String tempUrl = HttpSenderUtil.getTargetUrl(linkContent2);//商品目标来源
									//if (null != tempUrl && tempUrl.indexOf(soureUrl) > -1){
										//if (!tempUrl.endsWith("/")){
											//tempUrl += "/";
										//}
									  // tempUrl = HttpSenderUtil.getTargetUrl(tempUrl);//商品目标来源
								//	}
								//	if (null == tempUrl){
									//	tempUrl = "";	
									//}
									//subStract = subStract.replaceAll(linkContent, tempUrl);
								//	linkContent = cl.getAttributeValue("target");
								//	if(null !=linkContent){
									    //subStract = subStract.replaceAll(linkContent, "_self");
								//	}
								}
								subStract.replaceAll(soureUrl, "www.buymefan.com");
								List<Element> imgsList = bizElement.getAllElements(HTMLElementName.IMG);
								if(null != imgsList)
								for (Element e : imgsList){
									if (CommonUtil.isEmpty(imgUrl2)){
										imgUrl2 = e.getAttributeValue("src");
									}
									subStract = subStract.replace(e.toString(), "");
								}
							}
							
							if(CommonUtil.isEmpty(imgUrl)){
								imgUrl = imgUrl2;
							}
							
							targetUrl = CommonUtil.getFinalUrl(targetUrl);
							if (null != targetUrl){
								if (targetUrl.contains(soureUrl)){
									continue;
								}
								if (targetUrl.startsWith("ttp://")){
									targetUrl = "h" + targetUrl;
								}
							    //targetUrl = URLDecoder.decode(targetUrl);
							}
							
							//检查库中是否有这样的数据
							params.put("title", title);
							params.put("soureUrl", soureUrl);
							params.put("targetUrl", targetUrl);
							count = defaultService.find_iBatis("omg.crawler.checkGoodsIsExist", params);
							if (CommonUtil.isNotEmpty(count)){
								if (count.get(0) > 0){
									continue;
								}
							}
							
							
							postMeta.setMetaKey("thumbnail");
							postMeta.setMetaValue(imgUrl);
							postMeta2.setMetaKey("targeturl");
							postMeta2.setMetaValue(targetUrl);
							//postMeta.setId(UUID.randomUUID().toString());
							//postMeta2.setId(UUID.randomUUID().toString());
							posts.setSourceUrl(url);
							posts.setPostContent(subStract);
							posts.setPostName(title);
							
							//int postId = 0;
							if (CommonUtil.isNotEmpty(title) && CommonUtil.isNotEmpty(subStract) && CommonUtil.isNotEmpty(targetUrl)){
								log.info(url + "入库开始");
								pmSet.add(postMeta);
								pmSet.add(postMeta2);
								//posts.setPostMetas(pmSet);
							    //defaultService.save_Hibernate_Object(posts);
							    WriteDBUtil.writeToDb(posts, pmSet, type, tag, ",", ",",imgUrl, wbs);
							    pmSet.clear(); //清空扩展
							   /* postId = posts.getId();
							    if (postId <= 0){
							    	log.debug("生成的post_id 为:" + postId);
							    }
							    log.debug("post_id:" + postId);
							    this.saveCataAndTag(type, tag, postId);
							    if ("1".equals(PropertiesHelper.getProperty(ISSENDWEIBO))){
							       //发送微博
							        wbs.send(title, imgUrl, CrawlerStrategy.SITEURL+postId);
							    }*/
							}
							log.info(url + "商品录入成功.");
					   }
				  }
				  catch (Exception e) {
						e.printStackTrace();
						log.error("录入商品失败", e);
				  }
				  finally{
					  pmSet.clear(); //清空扩展
				  }
			   }
			}
		}
		catch (MalformedURLException e) {
			e.printStackTrace();
			log.error("录入商品失败2", e);
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
   

	public IDefaultService getDefaultService() {
		return defaultService;
	}

	public void setDefaultService(IDefaultService defaultService) {
		this.defaultService = defaultService;
	}
	public static void main(String[] args){
		RequestContext requestContext = new RequestContext();
		requestContext.setStartIndex(2);
		requestContext.setSiteUrl("http://zhizhizhi.com/");
		new ZhiZhiZhiStrategyImpl().handle(requestContext);
	}
	
}
